import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/site/HomeEnhanced.vue'),
      meta: {
        title: 'XappTool · 免邀请码安装归因 · 多平台SDK',
        keywords: 'XappTool, 安装归因, 免邀请码, SDK, 渠道统计, 指纹, 反作弊',
        description: '快速集成多平台SDK，精准识别安装来源，内置反作弊与隐私合规，助力业务增长。'
      }
    },
    {
      path: '/price',
      name: 'price',
      component: () => import('@/views/site/Price.vue'),
      meta: {
        title: '服务价格 · 灵活方案',
        keywords: '价格, 套餐, 企业版, 专业版',
        description: '灵活定价满足不同规模需求，支持企业级定制与私域部署。'
      }
    },
    {
      path: '/docs',
      name: 'docs',
      component: () => import('@/views/site/SDKDocs.vue'),
      meta: {
        title: 'SDK 文档 · 快速接入',
        keywords: 'SDK 文档, Web SDK, React SDK, UniApp SDK',
        description: '5分钟完成接入，提供多平台代码示例与最佳实践。'
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/site/Login.vue'),
      meta: {
        title: '登录 · 商户平台',
        keywords: '登录, 商户平台, 账户',
        description: '登录商户平台，管理应用与查看数据统计。'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/site/Register.vue'),
      meta: {
        title: '注册 · 商户平台',
        keywords: '注册, 商户平台, 账户',
        description: '注册商户账户，获取API Key并开始接入SDK。'
      }
    },
    {
      path: '/console',
      name: 'console',
      component: () => import('@/views/console/ConsoleEnhanced.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/console/detail',
      name: 'consoleDetail',
      component: () => import('@/views/console/AppDetail.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/console/stats',
      name: 'consoleStats',
      component: () => import('@/views/console/Stats.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/console/create',
      name: 'consoleCreate',
      component: () => import('@/views/console/Create.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/console/update',
      name: 'consoleUpdate',
      component: () => import('@/views/console/Update.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/console/test',
      name: 'consoleTest',
      component: () => import('@/views/console/Test.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/membership',
      name: 'membership',
      component: () => import('@/views/membership/Center.vue'),
      meta: { 
        requiresAuth: true,
        title: '会员中心 · 管理您的会员服务',
        keywords: '会员中心, 会员服务, 账单管理',
        description: '查看会员状态、管理账单、充值余额，享受专属会员权益。'
      }
    },
    {
      path: '/billing',
      name: 'billing',
      component: () => import('@/views/membership/Billing.vue'),
      meta: { 
        requiresAuth: true,
        title: '我的账单 · 账单记录管理',
        keywords: '账单, 充值记录, 消费记录',
        description: '查看您的账单记录、充值历史和消费明细。'
      }
    },
    {
      path: '/lp/:id',
      name: 'landingUnified',
      component: () => import('@/views/site/LandingUnified.vue')
    },
    {
      path: '/lp/custom/:id',
      name: 'landingCustom',
      component: () => import('@/views/site/LandingCustom.vue')
    }
  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const token = localStorage.getItem('token')

  // 需要登录的页面
  if (to.meta.requiresAuth) {
    if (!token) return next('/login')
  }

  // 已登录用户不能访问登录和注册页面
  if ((to.path === '/login' || to.path === '/register') && token) return next('/console')

  if (to.meta && (to.meta.title || to.meta.description || to.meta.keywords)) {
    if (to.meta.title) document.title = to.meta.title
    const setMeta = (name, content) => {
      let m = document.querySelector(`meta[name="${name}"]`)
      if (!m) {
        m = document.createElement('meta')
        m.setAttribute('name', name)
        document.head.appendChild(m)
      }
      m.setAttribute('content', content)
    }
    if (to.meta.description) setMeta('description', to.meta.description)
    if (to.meta.keywords) setMeta('keywords', to.meta.keywords)
  }

  next()
})

export default router